package com.iotmonitor.iot.controller;


import javax.servlet.http.HttpServletResponse;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.iotmonitor.common.core.utils.poi.ExcelUtil;
import com.iotmonitor.iot.domain.TbAlarm;
import com.iotmonitor.iot.domain.dto.AlarmMsg;
import com.iotmonitor.iot.service.ITbAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.iotmonitor.common.log.annotation.Log;
import com.iotmonitor.common.log.enums.BusinessType;
import com.iotmonitor.common.security.annotation.RequiresPermissions;
import com.iotmonitor.common.core.web.controller.BaseController;
import com.iotmonitor.common.core.web.domain.AjaxResult;
import com.iotmonitor.common.core.web.page.TableDataInfo;
import java.util.List;

/**
 * 报警配置Controller
 * 
 * @author ruoyi
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/alarm")
@ApiSort(4)
@Api(tags = "报警",value = "AlarmController")
public class TbAlarmController extends BaseController
{
    @Autowired
    private ITbAlarmService tbAlarmService;

    /**
     * 查询报警配置列表  query, header, path, cookie, form, formData, body
     */
    @RequiresPermissions("system:alarm:list")
    @GetMapping("/list")
    @ApiOperation(value = "报警列表",notes = "报警列表")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "query", name = "file", value = "图片上传接口参数", required = true) })
    @ApiResponses({ @ApiResponse(responseCode = "0", description = "操作成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部异常")})
    public TableDataInfo list(TbAlarm tbAlarm)
    {
        startPage();
        List<TbAlarm> list = tbAlarmService.selectTbAlarmList(tbAlarm);
        return getDataTable(list);
    }

    /**
     * 导出报警配置列表
     */
    @RequiresPermissions("system:alarm:export")
    @Log(title = "报警配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAlarm tbAlarm)
    {
        List<TbAlarm> list = tbAlarmService.selectTbAlarmList(tbAlarm);
        ExcelUtil<TbAlarm> util = new ExcelUtil<TbAlarm>(TbAlarm.class);
        util.exportExcel(response, list, "报警配置数据");
    }

    /**
     * 获取报警配置详细信息
     */
    @RequiresPermissions("system:alarm:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbAlarmService.selectTbAlarmById(id));
    }

    /**
     * 新增报警配置
     */
    @RequiresPermissions("system:alarm:add")
    @Log(title = "报警配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TbAlarm tbAlarm)
    {
        List<TbAlarm> tbAlarms = tbAlarmService.selectTbAlarmList(new TbAlarm(tbAlarm.getName()));
        if (!tbAlarms.isEmpty()){
            return error("已存在该名称");
        }
        return toAjax(tbAlarmService.insertTbAlarm(tbAlarm));
    }

    /**
     * 修改报警配置
     */
    @RequiresPermissions("system:alarm:edit")
    @Log(title = "报警配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TbAlarm tbAlarm)
    {
        return toAjax(tbAlarmService.updateTbAlarm(tbAlarm));
    }

    /**
     * 删除报警配置
     */
    @RequiresPermissions("system:alarm:remove")
    @Log(title = "报警配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbAlarmService.deleteTbAlarmByIds(ids));
    }

    /**
     * 告警日志查询
     * @param alarmName
     * @param deviceId
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/log")
    public TableDataInfo alarmLog(@RequestParam(value = "alarmName",required = false) String alarmName, @RequestParam(value = "deviceId",required = false) String deviceId, @RequestParam(value = "start",required = false) String start, @RequestParam(value = "end",required = false) String end) {
        startPage();
        return getDataTable(tbAlarmService.queryAlarmLog(alarmName, deviceId, start, end));
    }

}
